<?php
    require APPPATH .'third_party/admin_controller'.EXT;
    class News extends Admin_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function index()
        {
            $this->check_login_admin();
            $data = array();
            $data['show'] = $this->news_cat_model->show();
            $this->load_render($this->load->view('news/show',$data,TRUE));
        }
        public function add_update()
        {
            $this->check_login_admin();
            $data = array();
            $valid = $this->form_validation;
            $valid->set_rules('txt_name_cat','Title','trim|required|xss_clean');
            $valid->set_rules('txt_order','order','trim|required|xss_clean');
            $valid->set_rules('txt_active','active','trim|required|xss_clean');			
            $id = $this->uri->segment('4');
            if($valid->run() == TRUE)
            {
                $data = array(
                'name_cat' => $this->input->post('txt_name_cat'),
                'order_news' =>  $this->input->post('txt_order'),
                'active'=>  $this->input->post('txt_active'),
                'url'   => $this->util->alias($this->input->post('txt_name_cat'))
                );
                if($id !='')
                {
                    $this->news_cat_model->update_news($id,$data);
                    $data['error'] = "Cap nhat thanh cong";
                }
                else
                {
                    $this->news_cat_model->add_news($data);
                    $data['error'] = "Them thanh cong";
                }
                redirect('admin/news/');
            }
			$data['uid'] = $id;
			$data['show'] = $this->news_cat_model->get_id($id);
            $this->load_render($this->load->view('news/add',$data,TRUE));
        }
        public function delete_news()
        {
            $this->check_login_admin();
            $data = array();
            $id = $this->uri->segment('4');
            if($this->news_cat_model->delete_id($id))
            {
                redirect('admin/news');
            }
        }
        public function change_update_active()
        {
            $this->check_login_admin();
            $data = array();
            $id = $this->input->get('id');
            $data['active'] = $this->input->get('active');
            if($this->news_cat_model->change_update_active($id,$data))
            {
                $data['error'] = "Cập nhật thành công";
            }
            echo json_encode($data);
        }
    }
?>